package cn.itcast.bos.web.action.take_delivery;

import java.io.File;
import java.io.IOException;
import java.util.UUID;

import org.apache.commons.io.FileUtils;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.take_delivery.Promotion;
import cn.itcast.bos.service.take_delivery.PromotionService;
import cn.itcast.bos.web.action.common.BaseAction;

/**
 * 宣傳活動
 * 
 * @author FEI
 *
 */
@ParentPackage("json-default")
@Namespace("/")
@Controller
@Scope("prototype")
public class PromotionAction extends BaseAction<Promotion> {
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private File titleImgFile;
	private String titleImgFileFileName;

	public void setTitleImgFile(File titleImgFile) {
		this.titleImgFile = titleImgFile;
	}

	public void setTitleImgFileFileName(String titleImgFileFileName) {
		this.titleImgFileFileName = titleImgFileFileName;
	}

	@Autowired
	private PromotionService promotionService;

	@Action(value = "promotion_save", results = {
			@Result(name = "success", type = "redirect", location = "./pages/take_delivery/promotion.html") })
	public String save() throws IOException {
		// 宣傳圖上傳，在數據表保存宣傳圖路徑
		String savePath = ServletActionContext.getServletContext().getRealPath("/upload/");
		String saveUrl = ServletActionContext.getRequest().getContextPath() + "/upload/";
		// 生產隨機圖片名
		UUID uuid = UUID.randomUUID();
		String ext = titleImgFileFileName.substring(titleImgFileFileName.lastIndexOf("."));
		String randomFileName = uuid + ext;

		// 保存圖片（絕對路徑）
		File destFile = new File(savePath + "/" + randomFileName);
		System.out.println(destFile.getAbsolutePath());
		FileUtils.copyFile(titleImgFile, destFile);
		// 將保存路徑 相對工程web訪問路徑，保存model中
		model.setTitleImg(ServletActionContext.getRequest().getContextPath() + "/upload/" + randomFileName);
		// 調用業務層，完成活動任務數據保存
		promotionService.save(model);
		return SUCCESS;
	}
	@Action(value="promotion_pageQuery",
			results = {@Result(name="success",type="json")})
   public String pageQuery(){		
		//构造分页查询参数
		Pageable pageable = new PageRequest(page-1,rows);
		//调用业务层 完成查询
		Page<Promotion> pageData = promotionService.findPageData(pageable);
		//压入值栈
    	pushPageDataToValueStack(pageData);
    	System.out.println(pageData);
        return SUCCESS;
	}
}
